const path = require("path");

// 在vue-config.js 中加入
// 开启gzip压缩
const CompressionWebpackPlugin = require("compression-webpack-plugin");
// html插件
const HtmlWebpackPlugin = require("html-webpack-plugin");
// 判断开发环境
// const isProduction = process.env.NODE_ENV === 'production'

const resolve = dir => {
  return path.join(__dirname, dir);
};

const BASE_URL = process.env.NODE_ENV === "production" ? "/" : "/";

module.exports = {
  // 打包时不生成.map文件
  productionSourceMap: false,
  publicPath: BASE_URL,
  css: {
    loaderOptions: {
      scss: {}
    }
  },
  // webpack配置
  configureWebpack: config => {
    config.devtool = "source-map";
    const htmlPlugin = new HtmlWebpackPlugin({
      title: "Wevoov Admin",
      template: "public/index.html",
      url: "/",
      css: [],
      js: [],
      inject: "body",
      mete: {
        viewport: "width=device-width, initial-scale=1, user-scalable=no"
      },
      minify: {
        // 压缩HTML文件
        removeComments: true, // 移除HTML中的注释
        collapseWhitespace: true, // 删除空白符与换行符
        // 为了使GAEA能正确识别script, 保留引号
        // removeAttributeQuotes: true,
        minifyJS: true,
        removeScriptTypeAttributes: true,
        removeStyleLinkTypeAttributes: true
      }
    });
    config.plugins.push(htmlPlugin);

    // 开启gzip压缩
    // if (isProduction) {
    const gzip = new CompressionWebpackPlugin({
      algorithm: "gzip",
      test: /\.js$|\.html$|\.json$|\.css/,
      threshold: 10240,
      minRatio: 0.8
    });

    config.plugins.push(gzip);

    // 开启分离js
    config.optimization = {
      runtimeChunk: "single",
      splitChunks: {
        chunks: "all",
        maxInitialRequests: Infinity,
        minSize: 20000,
        cacheGroups: {
          vendor: {
            test: /[\\/]node_modules[\\/]/,
            name(module) {
              // get the name. E.g. node_modules/packageName/not/this/part.js
              // or node_modules/packageName
              const packageName = module.context.match(
                /[\\/]node_modules[\\/](.*?)([\\/]|$)/
              )[1];
              // npm package names are URL-safe, but some servers don't like @ symbols
              return `npm.${packageName.replace(
                "@",
                ""
              )}-${new Date().getTime()}`;
            }
          }
        }
      }
    };
    // 取消webpack警告的性能提示
    config.performance = {
      hints: "warning",
      // 入口起点的最大体积
      maxEntrypointSize: 50000000,
      // 生成文件的最大体积
      maxAssetSize: 30000000,
      // 只给出 js 文件的性能提示
      assetFilter: function(assetFilename) {
        return assetFilename.endsWith(".js");
      }
    };
    // }
  },

  devServer: {
    host: "localhost",
    port: 8080, // 端口号
    hotOnly: false,
    https: false, // https:{type:Boolean}
    open: true // 配置自动启动浏览器
  },
  lintOnSave: true,
  chainWebpack: config => {
    config.resolve.alias
      .set("@a", resolve("./src/api"))
      .set("@v", resolve("./src/views"))
      .set("@u", resolve("./src/utils"))
      .set("@m", resolve("./src/mixins"))
      .set("@c", resolve("./src/components"));
    config.module
      .rule("vue")
      .test(/\.vue$/)
      .use("iview-loader")
      .loader("iview-loader")
      .options({
        prefix: true
      });
  }
};
